package com.atjg.autocat.camera;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.os.AsyncTask;
import android.util.Log;

import java.nio.ByteBuffer;

 class ImageSaveTask extends AsyncTask<Image, Void, Bitmap> {

     @Override
     protected Bitmap doInBackground(Image... images) {
         ByteBuffer buffer = images[0].getPlanes()[0].getBuffer();
         byte[] bytes = new byte[buffer.remaining()];
         buffer.get(bytes);

         long time = System.currentTimeMillis();
//         if (mCameraProxy.isFrontCamera()) {
             Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
//             Log.d(TAG, "BitmapFactory.decodeByteArray time: " + (System.currentTimeMillis() - time));
             time = System.currentTimeMillis();
             // 前置摄像头需要左右镜像
             Bitmap rotateBitmap = ImageUtils.rotateBitmap(bitmap, 0, true, true);
//             Log.d(TAG, "rotateBitmap time: " + (System.currentTimeMillis() - time));
             time = System.currentTimeMillis();
//             ImageUtils.saveBitmap(rotateBitmap);
//             Log.d(TAG, "saveBitmap time: " + (System.currentTimeMillis() - time));
             rotateBitmap.recycle();
//         } else {
//             ImageUtils.saveBitmap(bytes);
//             Log.d(TAG, "saveBitmap time: " + (System.currentTimeMillis() - time));
//         }
         images[0].close();
         return bitmap;
//         return ImageUtils.getLatestThumbBitmap();
     }

     @Override
     protected void onPostExecute(Bitmap bitmap) {

     }
 }
